package com.Glow.reggie.service.impl;

import com.Glow.reggie.common.CustomException;
import com.Glow.reggie.dto.SetmealDto;
import com.Glow.reggie.entity.Setmeal;
import com.Glow.reggie.entity.SetmealDish;
import com.Glow.reggie.mapper.SetmealMapper;
import com.Glow.reggie.service.SetmealDishService;
import com.Glow.reggie.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Glow
 * @Date 2022-07-30 21:58:29
 * @Description
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {
   @Autowired
   private SetmealDishService setmealDishService;

    /**
     * 保存套餐菜品信息，并把口味相关联
     * @param setmealDto
     */
    @Override
    @Transactional
    public void save(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        setmeal = setmealDto;
        this.save(setmeal);
        Long SetmealId =setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(item->{
            SetmealDish setmealDish = new SetmealDish();
            BeanUtils.copyProperties(item,setmealDish);
            setmealDish.setSetmealId(SetmealId);
            return setmealDish;

        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

    }

    @Override
    @Transactional
    public void update(SetmealDto setmealDto) {
        Setmeal setmeal = new Setmeal();
        setmeal = setmealDto;
        this.updateById(setmeal);
        //还要先删除菜单里的菜品再更新过去
        LambdaQueryWrapper<SetmealDish> queryWrapperDish = new LambdaQueryWrapper<>();
        queryWrapperDish.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapperDish);
        List<SetmealDish>list = setmealDto.getSetmealDishes();
        list =list.stream().map(item->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(list);
    }
    @Transactional
    @Override
    public void remove(String ids) {
        String[]spilts = ids.split(",");
        for(String id:spilts)
        {
            this.removeById(id);
            LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(queryWrapper);
        }

    }
    @Transactional
    @Override
    public void status(Integer status, String ids) {
        String[]split = ids.split(",");
        for (String id:split)
        {
            Long Id = Long.valueOf(id);
            Setmeal setmeal = this.getById(Id);
            setmeal.setStatus(status);
            this.updateById(setmeal);
        }
    }

    /**
     * 删除套餐，同时需要删除套餐和菜品的关联数据
     * @param ids
     */
    @Transactional
    @Override
    public void removeWithDish(List<Long> ids) {
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if(count > 0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐表中的数据---setmeal
        this.removeByIds(ids);

        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除关系表中的数据----setmeal_dish
        setmealDishService.remove(lambdaQueryWrapper);
    }



}
